<?php

namespace common\models\work;

use Yii;
use common\helpers\StringHelper;

/**
 * This is the model class for table "{{%staff}}".
 *
 * @property int $id 员工ID
 * @property int $user_id 用户ID
 * @property string $realname 姓名
 * @property string $mobile 手机号
 * @property string $mobile_hash 加密手机号
 * @property int $company_id 隶属单位ID
 * @property int $status 状态
 * @property int $created_at 注册时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 注销时间
 */
class Staff extends \common\db\ActiveRecord {

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%staff}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'company_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['realname'], 'required'],
            [['realname'], 'string', 'max' => 50],
            [['mobile'], 'string', 'max' => 32],
            [['mobile_hash'], 'string', 'max' => 255],
            [['mobile'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'realname' => '姓名',
            'mobile' => '手机号（唯一）',
            'mobile_hash' => '手机号',
            'company_id' => '单位ID',
            'status' => '绑定状态',
            'created_at' => '添加时间',
            'updated_at' => '最后更新时间',
            'deleted_at' => '删除时间',
            //
            'maskMobile' => '手机号',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getCompany() {
        return $this->hasOne(\common\models\survey\Company::class, ['id' => 'company_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCompanyName() {
        $company = $this->company;
        if (!$company) {
            return '';
        }
        return $company->name;
    }

    /**
     * {@inheritdoc}
     */
    public function getParentCompanyName() {
        $company = $this->company;
        if (!$company) {
            return null;
        }
        $cityCompany = $company->parent;
        if (!$cityCompany) {
            return null;
        }
        return $cityCompany->name;
    }

    /**
     * {@inheritdoc}
     */
    public function getCompanyNames() {
        $company = $this->company;
        if (!$company) {
            return [];
        }
        $cityCompany = $company->parent;
        if (!$cityCompany || $cityCompany->name == $company->name) {
            return [$company->name];
        }
        return [
            $cityCompany->name,
            $company->name,
        ];
    }

    /**
     * 关联交易用户信息
     * @return \common\db\ActiveQuery
     */
    public function getUser() {
        return $this->hasOne(\common\logics\UserCellLogic::class, ['id' => 'user_id']);
    }

    /**
     * 解密手机号
     * @param boolean $mask
     * @return string 手机号
     */
    public function decryptMobile($mask = true) {
        /* @var $security \common\base\Security */
        $security = Yii::$app->security;
        $mobile = $security->decryptMaster($this->mobile_hash);
        if ($mask !== false) {
            return StringHelper::maskChar($mobile, 3, 2);
        }
        return $mobile;
    }

    /**
     * 设置手机号（自动加密）
     * @param string $mobile 手机号
     */
    public function setMobile($mobile) {
        if ($mobile) {
            /* @var $security \common\base\Security */
            $security = Yii::$app->security;
            $this->mobile = $security->generatePrivateKey($mobile);
            $this->mobile_hash = $security->encryptMaster($mobile);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getMaskMobile() {
        return $this->decryptMobile();
    }

    /**
     * {@inheritdoc}
     */
    public function getFullMobile() {
        return $this->decryptMobile(false);
    }

    /**
     * 根据手机号查询用户信息
     * @param string $mobile
     * @return static|null
     */
    public static function findByMobile($mobile) {
        $entra = Yii::$app->security->generatePrivateKey($mobile);
        return static::find()->andWhere([
            'mobile' => $entra,
        ]);
    }

}
